package com.springboot.framework.service.impl;

import com.springboot.framework.dao.entity.Seller;
import com.springboot.framework.dao.mapper.SellerMapper;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.enums.StatusEnum;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.ProductModel;
import com.springboot.framework.model.SellerModel;
import com.springboot.framework.model.ShopModel;
import com.springboot.framework.service.ProductService;
import com.springboot.framework.service.SellerService;
import com.springboot.framework.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author SWF
 * @Date 2019/6/8 23:11
 **/
@Service
public class SellerServiceImpl implements SellerService {

    @Resource
    private SellerMapper sellerMapper;

    @Resource
    private ShopService shopService;
    /**
     * openID登录
     *
     * @param openId
     * @return
     */
    @Override
    public SellerModel get(String openId) {
        Example example = new Example(Seller.class);
        example.and().andEqualTo("wechatOpenid",openId);
        Seller seller = sellerMapper.selectOneByExample(example);
        return convertSellerModelFromSeller(seller);
    }

    @Override
    public SellerModel insert(SellerModel sellerModel) {
        // 数据有效性校验
        Seller seller = convertSeller(sellerModel);
        seller.setStatus(StatusEnum.DISABLED.getCode());
        int resultCount = sellerMapper.insertSelective(seller);
        if(resultCount == 0){
            throw new BusinessException(Errors.SYSTEM_INSERT_FAIL);
        }
        return convertSellerModelFromSeller(seller);
    }

    @Override
    public List<SellerModel> listByShopId(Integer shopId) {
        Example example = new Example(Seller.class);
        example.and().andEqualTo("shopId" , shopId)
                .andEqualTo("status" , StatusEnum.NORMAL.getCode());
        List<Seller> sellers = sellerMapper.selectByExample(example);
        return convertSellerModelFromSeller(sellers);
    }

    @Override
    public List<SellerModel> listAllByShopId(Integer shopId) {
        Example example = new Example(Seller.class);
        example.and().andEqualTo("shopId" , shopId)
               .andNotEqualTo("status", StatusEnum.DELETE.getCode());
        List<Seller> sellers = sellerMapper.selectByExample(example);
        return convertSellerModelFromSeller(sellers);
    }

    @Override
    public List<SellerModel> list() {
        Example example = new Example(Seller.class);
        example.and().andNotEqualTo("status" , StatusEnum.DELETE.getCode());
        List<Seller> sellers = sellerMapper.selectByExample(example);
        return convertSellerModelFromSeller(sellers);
    }

    @Override
    public SellerModel update(SellerModel sellerModel) {
        Seller seller = convertSeller(sellerModel) ;
        sellerMapper.updateByPrimaryKeySelective(seller);
        return convertSellerModelFromSeller(seller);
    }

    @Override
    public SellerModel updateStatus(SellerModel sellerModel) {
        Seller seller = new Seller();
        seller.setId(sellerModel.getId());
        seller.setStatus(sellerModel.getStatus());
        seller.setCreateBy(sellerModel.getCreateBy());
        sellerMapper.updateByPrimaryKeySelective(seller);
        return convertSellerModelFromSeller(seller);
    }


    private SellerModel convertSellerModelFromSeller(Seller seller){
        if(seller == null){
            return null;
        }
        SellerModel sellerModel = new SellerModel();
        BeanUtils.copyProperties(seller,sellerModel);
        sellerModel.setUserType(StatusEnum.SELLER_USER.getCode());
        if(sellerModel.getShopId() != null){
            ShopModel shopModel = shopService.get(sellerModel.getShopId());
            if(shopModel == null){
                sellerModel.setShopName("店铺已被删除");
            }else{
                sellerModel.setShopName(shopModel.getName());
            }
        }
        return sellerModel;
    }

    private List<SellerModel> convertSellerModelFromSeller(List<Seller> sellers){
        if(sellers == null){
            return null;
        }
        List<SellerModel> sellerModel = sellers.stream()
                .map( e->convertSellerModelFromSeller(e))
                .collect(Collectors.toList());
        return sellerModel;
    }

    private Seller convertSeller(SellerModel sellerModel){
        Seller seller = new Seller();
        BeanUtils.copyProperties(sellerModel,seller);
        return seller;
    }
}
